package com.sxs.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxs.model.Menu;
import com.sxs.model.VaultNavigate;
import com.sxs.service.LoginServiceInterface;

import utils.JSONSerializer;
import utils.LimitsOfAuthority;
import utils.Utils;


@Controller
@RequestMapping(value = "/login")
public class LoginController {
	Logger logger = Logger.getLogger(LoginController.class);
	private LoginServiceInterface loginService;

	@Autowired
	public void setLoginService(LoginServiceInterface loginService) {
		this.loginService = loginService;
	} 
	
	/**
	 * 登录方法
	 * @param session  用户权限参数
	 * @param request  请求参数 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(HttpServletRequest request, HttpSession session) {
		 logger.info(Thread.currentThread().getStackTrace()[1].getMethodName() + "==start");
		 String username = request.getParameter("username");
		 String password = request.getParameter("password");
		 logger.info("---获取登录用户信息---username：" + username + ",password:" + password);
		 session.putValue("username", username);
//		 if(!Utils.checkStrNull(username) && !Utils.checkStrNull(password) ){
//			 //对密码加密
//			 password = Utils.string2MD5(password);
//			 String end = loginService.getVerifylogin(username, password, request, session);
//			 if(end.equals("true")){
			   return "/frame/main";
//			 }
//		 }
//		 logger.info(Thread.currentThread().getStackTrace()[1].getMethodName() + "==end");
//		 return "redirect:../login.jsp";
	}
	
	

	/**
	 * 左侧列表导航自动加载
	 * 
	 * @param id 父ID
	 * @param session  数据参数s
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/MUList", produces = { "text/javascript;charset=UTF-8" })
	@ResponseBody
	public String liftList(String id, HttpSession session) {
		Map<String, List<String>> listSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
		List<Menu> ListVN = loginService.getLeftList(id, session);
		return JSONSerializer.serialize(ListVN);

	}

	/**
	 * 左侧导航跳转加载
	 * 
	 * @param id  付ID
	 * @param session  数据参数
	 * @param model  返回模型参数
	 * @return
	 */
	@RequestMapping(value = "/MUList2")
	public String liftList2(String id, HttpSession session, Model model, HttpServletRequest request) {
		List<Menu> ListVN = loginService.getLeftList(id, session);
		String returnStr = LimitsOfAuthority.getJointStr(ListVN, request);
		model.addAttribute("MUList2", returnStr);
		return "frame/leftSkip";
	}
	/**
	 * 退出系统（登出）
	 * @param session  用户权限参数
	 * @return
	 */
	@RequestMapping(value = "/loginExit", method = RequestMethod.GET)
	public String exitLogin(HttpSession session) {
		logger.info(Thread.currentThread().getStackTrace()[1].getMethodName() + "==start");
		session.removeAttribute("userID"); 
		logger.info(Thread.currentThread().getStackTrace()[1].getMethodName() + "==end");
		return  "redirect:http://www.shaxiaoseng.com";
	}

	/**
	 * 跳转方法
	 * 
	 * @param model 模型参数
	 * @return
	 */
	@RequestMapping(value = "/right", method = RequestMethod.GET)
	public String right(Model model) {
		return "/frame/right";
	}
	/**
	 * 跳转左侧导航
	 * 
	 * @param model 模型参数
	 * @return
	 */
	@RequestMapping(value = "/left", method = RequestMethod.GET)
	public String left(Model model) {
		return "/frame/left";
	}
	
	/**
	 * 跳转组织结构导航
	 * 
	 * @param model 模型参数
	 * @return
	 */
	@RequestMapping(value = "/leftMenu", method = RequestMethod.GET)
	public String leftMenu(Model model) {
		return "/frame/menu";
	}
	
}
